package com.example.demo.controller;

import com.example.demo.domain.SysUser;
import com.example.demo.domain.SysUserExample;
import com.example.demo.service.SysUserService;
import com.example.demo.service.domain.UserListResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    SysUserService sysUserService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public UserListResp list(@RequestParam(value = "page") Integer page, @RequestParam(value = "num") Integer num) {
        UserListResp responseBean = new UserListResp();
        page = (page - 1) * num;
        SysUserExample sysUserExample = new SysUserExample();
        sysUserExample.setOrderByClause("user_id desc limit " + page + "," + num);
        List<SysUser> sysUserList = sysUserService.selectByExample(sysUserExample);
        responseBean.setData(sysUserList);
        // total
        long total = sysUserService.countByExample(sysUserExample);
        responseBean.setTotal(total);
        return responseBean;
    }

}
